package com.pig4cloud.pigx.ruralLibrary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.ruralLibrary.api.entity.BookUserEntity;
import com.pig4cloud.pigx.ruralLibrary.api.entity.time;
import com.pig4cloud.pigx.ruralLibrary.api.dto.townshipUserStatisticsVo;
import com.pig4cloud.pigx.ruralLibrary.service.informationStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/informationStatistics")
@Tag(description = "informationStatistics", name = "信息统计接口")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class informationStatisticsController {

	private final informationStatisticsService service;

	@Operation(summary = "总用户量", description = "总用户量")
	@GetMapping("/totalUsers")
	public R totalUsers(BookUserEntity bookUser) {
		return service.totalUsers(bookUser);
	}

	@Operation(summary = "日/月/浏览量", description = "日/月/浏览")
	@GetMapping("/browsePage")
	public R browseList() {
		return service.browseList();
	}

	@Operation(summary = "注册村的数量", description = "注册村的数量")
	@GetMapping("/registerList")
	public R registerList(BookUserEntity bookUser) {
		return service.registerList(bookUser);
	}

	@Operation(summary = "人 管理员", description = "人 管理员")
	@GetMapping("/peopleList")
	public R peopleList(BookUserEntity bookUser) {
		return service.peopleList(bookUser);
	}

	@Operation(summary = "资源分析", description = "资源分析")
	@GetMapping("/resourceAnalysis")
	public R resourceAnalysis(BookUserEntity bookUser) {
		return service.resourceAnalysis();
	}

	@Operation(summary = "访问来源", description = "访问来源")
	@GetMapping("/accessSource")
	public R accessSource(BookUserEntity bookUser) {
		return service.accessSource();
	}

	@Operation(summary = "统计新增用户", description = "统计新增用户")
	@PostMapping(value = "/getNewUsers")
	public R getNewUsers(@RequestBody time time) {
		return service.getNewUsers(time.getStartTime(), time.getEndTime());
	}
	@Operation(summary = "统计用户浏览量", description = "统计用户浏览量")
	@PostMapping(value = "/getRead")
	public R getRead(@RequestBody time time) {
		return service.getRead(time.getStartTime(), time.getEndTime());
	}
	@Operation(summary = "各区人员统计", description = "各区人员统计")
	@GetMapping(value = "/numberPeople")
	public R numberPeople(Page page, String name) {
		return service.numberPeople(page, name);
	}

	@Operation(summary = "活跃地区统计", description = "活跃地区统计")
	@GetMapping(value = "/activeArea")
	public R activeArea(Page page, String name) {
		return service.activeArea(page, name);
	}

	@Operation(summary = "乡镇用户统计", description = "乡镇用户统计")
	@GetMapping(value = "/townshipUserStatistics")
	public R townshipUserStatistics(Page page, townshipUserStatisticsVo townshipUserStatisticsVo) {
		List<townshipUserStatisticsVo> townshipUserStatisticsVos = service.townshipUserStatistics(page, townshipUserStatisticsVo);
		if (townshipUserStatisticsVo==null){
			return R.failed("没有权限");
		}
		return R.ok(page.setRecords(townshipUserStatisticsVos));
	}

	/**
	 * 导出excel 表格
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/townshipUserStatistics/export")
	@PreAuthorize("@pms.hasPermission('ruralLibrary_township_export')")
	public List<townshipUserStatisticsVo> export(townshipUserStatisticsVo townshipUserStatisticsVo) {
		return service.townshipUserStatistics(null,townshipUserStatisticsVo);
	}

	@GetMapping(value = "/Statistics")
	public Map Statistics() {
		return service.Statistics();
	}
}
